package com.BigDate.server.controller;


import com.BigDate.server.pojo.*;
import com.BigDate.server.service.ProjectCommentService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stx
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/project-comment")
public class ProjectCommentController {

    @Autowired
    private ProjectCommentService commentService;

    @ApiOperation(value = "通过id获取")
    @GetMapping("findComment/{projectId}")
    public RespBean findComment(@PathVariable Integer projectId)
    {
        List<CommentNodeTreeVo> comment = commentService.findComment(projectId);
        comment.forEach(System.out::println);
        for (CommentNodeTreeVo commentNodeTreeVo : comment) {
            deepSearch(commentNodeTreeVo);
        }
        return RespBean.success("200",comment);
    }

    @ApiOperation(value = "通过分页获取")
    @GetMapping("/findAll/{current}/{size}")
    public  RespBean findAll(@PathVariable Integer current,
                             @PathVariable Integer size)
    {
        System.out.println(current);
//        System.out.println(comment);
        System.out.println(size);
        Page<CommentNodeTreeVo> all = commentService.findAll(current, size);
        all.getRecords().forEach(System.out::println);
        return RespBean.success("分页查询成功",all);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public RespBean add(@RequestBody Comment comment)
    {
        System.out.println(comment);
        int res = commentService.add(comment);
        if (res == 1) {
            return RespBean.success("添加成功",200);
        }else{
            return RespBean.success("添加失败",404);
        }
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable("id") Integer id)
    {
        return RespBean.success("删除成功",commentService.deleteById(id));
    }

    private void deepSearch(CommentNodeTreeVo commentNodeTreeVo) {
        if (commentNodeTreeVo != null) {
            // 查询子评论集合
            List<CommentNodeTreeVo> commentNodeTreeVos = commentService.findChildComment(commentNodeTreeVo.getComment_id());
            if (!CollectionUtils.isEmpty(commentNodeTreeVos)) {
                for (CommentNodeTreeVo vo : commentNodeTreeVos) {
                    commentNodeTreeVo.setCommentNodeTreeVos(commentNodeTreeVos);
                    deepSearch(vo);
                }
            }
        }
    }




}
